package com.ruoyi.whzj.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.whzj.domain.*;
import com.ruoyi.whzj.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 任务控制器
 */
@Controller
@RequestMapping(value = "/phone/whzj/whLiveNotice")
public class WhLiveNoticePhoneController extends BaseController {

    @Autowired
    private IWhUserService whUserService;

    @Autowired
    private IWhzjLivenoticeService whzjLivenoticeService;

    @Autowired
    private IWhzjLivetypeService whzjLivetypeService;

    @Autowired
    private IWhzjLiveconfigService whzjLiveconfigService;

    @Autowired
    private IWhIntegralHistoryService whIntegralHistoryService;


    /**
     * 获取直播参数
     * @return
     * @throws Exception
     */
    @RequestMapping("/getLiveConfig")
    @ResponseBody
    public AjaxResult getLiveConfig()throws Exception
    {
        WhzjLiveconfig liveconfig = whzjLiveconfigService.selectWhzjLiveconfigById("1");
        return success("获取", liveconfig);

    }

    /**
     * 获取直播分类
     * @return
     */
    @RequestMapping("/getLiveType")
    @ResponseBody
    public AjaxResult getLiveType()throws Exception
    {
        List<WhzjLivetype> list = whzjLivetypeService.selectWhzjLivetypeList(new WhzjLivetype());
        return success("获取", list);

    }


    /**
     *  添加直播预告
     * @param userId
     * @param desc
     * @param type
     * @param begin
     * @param time
     * @param homeshow
     * @param integral
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveLiveNotice")
    @ResponseBody
    public AjaxResult saveLiveNotice(String userId, String desc, String type, String begin, Integer time, String homeshow, int integral)throws Exception
    {
        WhUser user = whUserService.selectWhUserById(userId);
        if("0".equals(user.getIsDisable())){
            return error("用户被封禁，请联系客服");
        }
        if(user != null){
            if(user.getIntegral() >= integral){



                WhzjLivenotice livenotice = new WhzjLivenotice();
                livenotice.setId(IdWorker.getId());
                livenotice.setUserId(userId);
                livenotice.setLiveDesc(desc);
                livenotice.setLiveType(type);
                String date = DateUtils.getDate() + " " + begin + ":00";
                livenotice.setLiveBegin(DateUtils.parseDate(date));
                livenotice.setLiveTime(time);
                livenotice.setIsHomeshow(homeshow);
                livenotice.setCreateTime(new Date());
                livenotice.setUpdateTime(new Date());
                if("0".equals(homeshow)){
                    WhzjLivenotice livenotice1 = whzjLivenoticeService.getMaxSort();
                    if(livenotice1 != null){
                        livenotice.setSort(livenotice1.getSort() + 1);
                    }else{
                        livenotice.setSort(1);
                    }
                }

                whzjLivenoticeService.insertWhzjLivenotice(livenotice);


                WhUser user1 = new WhUser();
                user1.setId(userId);
                user1.setIntegral(user.getIntegral() - integral);
                whUserService.updateWhUser(user1);

                WhIntegralHistory history = new WhIntegralHistory();
                history.setId(IdWorker.getId());
                history.setCreateTime(new Date());
                history.setUserId(userId);
                history.setIntegral(-integral);
                history.setEventId(livenotice.getId());
                history.setOperat("10");
                whIntegralHistoryService.insertWhIntegralHistory(history);

                return success("提交成功");

            }else{
                return error("用户积分不足");
            }
        }else{
            return error("用户不存在");
        }

    }


    /**
     * 获取直播预告
     * @return
     * @throws Exception
     */
    @RequestMapping("/getLiveList")
    @ResponseBody
    public AjaxResult getLiveList()throws Exception
    {
        List<WhzjLivenotice> list = whzjLivenoticeService.selectLiveList(new WhzjLivenotice());
        return success("获取", list);

    }

    /**
     * 获取首页直播预告
     * @return
     * @throws Exception
     */
    @RequestMapping("/getLiveListHome")
    @ResponseBody
    public AjaxResult getLiveListHome()throws Exception
    {
        WhzjLivenotice livenotice = new WhzjLivenotice();
        livenotice.setIsHomeshow("1");
        List<WhzjLivenotice> list = whzjLivenoticeService.selectLiveList(livenotice);
        return success("获取", list);

    }


    /**
     * 观看直播
     * @param id
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping("/seeLive")
    @ResponseBody
    public AjaxResult seeLive(String id, String userId)throws Exception
    {
        WhzjLivenotice livenotice = whzjLivenoticeService.selectWhzjLivenoticeById(id);

        if(EmptyUtils.isEmpty(livenotice.getUserids()) || !livenotice.getUserids().contains(userId)){
            WhzjLiveconfig liveconfig = whzjLiveconfigService.selectWhzjLiveconfigById("1");

            WhzjLivenotice livenotice1 = new WhzjLivenotice();
            livenotice1.setUserids(livenotice.getUserids() + "," + userId);
            livenotice1.setId(id);
            whzjLivenoticeService.updateWhzjLivenotice(livenotice1);

            WhUser user = whUserService.selectWhUserById(userId);

            WhUser user1 = new WhUser();
            user1.setId(userId);
            user1.setIntegral(user.getIntegral() + liveconfig.getLiveSend());
            whUserService.updateWhUser(user1);

            WhIntegralHistory history = new WhIntegralHistory();
            history.setId(IdWorker.getId());
            history.setCreateTime(new Date());
            history.setUserId(userId);
            history.setIntegral(liveconfig.getLiveSend());
            history.setEventId(livenotice.getId());
            history.setOperat("19");
            whIntegralHistoryService.insertWhIntegralHistory(history);
        }
        return success("");

    }


}

